 package server.cashshop;


import client.MapleClient;
import client.inventory.*;
import com.alibaba.druid.pool.DruidPooledConnection;
import constants.ItemConstants;
import database.DatabaseConnection;
import server.MapleInventoryManipulator;
import server.MapleItemInformationProvider;
import tools.DateUtil;
import tools.Pair;
import tools.packet.MTSCSPacket;

import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;





















 public class CashShop implements Serializable {
/*  24 */   private final ItemLoader factory = ItemLoader.现金道具; private static final long serialVersionUID = 231541893513373579L;
/*  25 */   private final List<Item> inventory = new ArrayList<>();
/*  26 */   private final List<Integer> uniqueids = new ArrayList<>();
   private int accountId;
   private int characterId;

   public CashShop() {}

   public CashShop(int accountId, int characterId, int jobType) throws SQLException {
/*  33 */     this.accountId = accountId;
/*  34 */     this.characterId = characterId;
/*  35 */     for (Pair<Item, MapleInventoryType> item : (Iterable<Pair<Item, MapleInventoryType>>)this.factory.loadItems(false, accountId).values()) {
/*  36 */       this.inventory.add(item.getLeft());
     }
   }

   public int getItemsSize() {
/*  41 */     return this.inventory.size();
   }

   public List<Item> getInventory() {
/*  45 */     return this.inventory;
   }

   public Item findByCashId(int cashId) {
/*  49 */     for (Item item : this.inventory) {
/*  50 */       if (item.getUniqueId() == cashId) {
/*  51 */         return item;
       }
     }
/*  54 */     return null;
   }

   public void checkExpire(MapleClient c) {
/*  58 */     List<Item> toberemove = new ArrayList<>();
/*  59 */     for (Item item : this.inventory) {
/*  60 */       if (item != null && !ItemConstants.isPet(item.getItemId()) && item.getExpiration() > 0L && item.getExpiration() < System.currentTimeMillis()) {
/*  61 */         toberemove.add(item);
       }
     }
/*  64 */     if (toberemove.size() > 0) {
/*  65 */       for (Item item : toberemove) {
/*  66 */         removeFromInventory(item);
/*  67 */         c.announce(MTSCSPacket.INSTANCE.cashItemExpired(item.getUniqueId()));
       }
/*  69 */       toberemove.clear();
     }
   }

   public Item toItem(CashItemInfo cItem) {
/*  74 */     return toItem(cItem, MapleInventoryManipulator.getUniqueId(cItem.getItemId(), null), "");
   }

   public Item toItem(CashItemInfo cItem, String gift) {
/*  78 */     return toItem(cItem, MapleInventoryManipulator.getUniqueId(cItem.getItemId(), null), gift);
   }

   public Item toItem(CashItemInfo cItem, int uniqueid) {
/*  82 */     return toItem(cItem, uniqueid, "");
   }

   public Item toItem(CashItemInfo cItem, int uniqueid, String gift) {
/*  86 */     if (uniqueid <= 0) {
/*  87 */       uniqueid = MapleInventoryIdentifier.getInstance();
     }
/*  89 */     long period = cItem.getPeriod();
/*  90 */     Item ret = null;
/*  91 */     if (ItemConstants.getInventoryType(cItem.getItemId()) == MapleInventoryType.EQUIP) {
/*  92 */       Equip eq = (Equip) MapleItemInformationProvider.getInstance().getEquipById(cItem.getItemId(), uniqueid);
/*  93 */       if (period > 0L) {
/*  94 */         eq.setExpiration(System.currentTimeMillis() + period * 24L * 60L * 60L * 1000L);
       }
/*  96 */       eq.setGMLog("商城购买 " + cItem.getSN() + " 时间 " + DateUtil.getCurrentDate());
/*  97 */       eq.setGiftFrom(gift);
/*  98 */       if (ItemConstants.isEffectRing(cItem.getItemId()) && uniqueid > 0) {
/*  99 */         MapleRing ring = MapleRing.loadFromDb(uniqueid);
/* 100 */         if (ring != null) {
/* 101 */           eq.setRing(ring);
         }
       }
/* 104 */       ret = eq.copy();
     } else {
/* 106 */       Item item = new Item(cItem.getItemId(), (short)0, (short)cItem.getCount(), (short)0, uniqueid, (short)0);
/* 107 */       if (ItemConstants.isPet(cItem.getItemId())) {
/* 108 */         period = 90L;
/* 109 */         item.setExpiration(System.currentTimeMillis() + period * 24L * 60L * 60L * 1000L);
/* 110 */       } else if (cItem.getItemId() == 5211047 || cItem.getItemId() == 5360014) {
/* 111 */         item.setExpiration(System.currentTimeMillis() + 10800000L);
/* 112 */       } else if (cItem.getItemId() == 5211060) {
/* 113 */         item.setExpiration(System.currentTimeMillis() + 7200000L);
/* 114 */       } else if (period > 0L) {
/* 115 */         item.setExpiration(System.currentTimeMillis() + period * 24L * 60L * 60L * 1000L);
       } else {
/* 117 */         item.setExpiration(-1L);
       }
/* 119 */       item.setGMLog("商城购买 " + cItem.getSN() + " 时间 " + DateUtil.getCurrentDate());
/* 120 */       item.setGiftFrom(gift);
/* 121 */       if (ItemConstants.isPet(cItem.getItemId())) {
/* 122 */         MaplePet pet = MaplePet.createPet(cItem.getItemId(), uniqueid);
/* 123 */         if (pet != null) {
/* 124 */           item.setPet(pet);
         }
       }
/* 127 */       ret = item.copy();
     }
/* 129 */     return ret;
   }

   public void addToInventory(Item item) {
/* 133 */     this.inventory.add(item);
   }

   public void removeFromInventory(Item item) {
/* 137 */     this.inventory.remove(item);
   }

   public void gift(int recipient, String from, String message, int sn) {
/* 141 */     gift(recipient, from, message, sn, 0);
   }

   public void gift(int recipient, String from, String message, int sn, int uniqueid) {
/* 145 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 146 */       PreparedStatement ps = con.prepareStatement("INSERT INTO `gifts` VALUES (DEFAULT, ?, ?, ?, ?, ?)");
/* 147 */       ps.setInt(1, recipient);
/* 148 */       ps.setString(2, from);
/* 149 */       ps.setString(3, message);
/* 150 */       ps.setInt(4, sn);
/* 151 */       ps.setInt(5, uniqueid);
/* 152 */       ps.executeUpdate();
/* 153 */       ps.close();
/* 154 */     } catch (SQLException sqle) {
/* 155 */       sqle.printStackTrace();
     }
   }

   public List<Pair<Item, String>> loadGifts() {
/* 160 */     List<Pair<Item, String>> gifts = new ArrayList<>();
/* 161 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/* 162 */       PreparedStatement ps = con.prepareStatement("SELECT * FROM `gifts` WHERE `recipient` = ?");
/* 163 */       ps.setInt(1, this.characterId);
/* 164 */       ResultSet rs = ps.executeQuery();

/* 166 */       while (rs.next()) {
/* 167 */         CashItemInfo cItem = CashItemFactory.getInstance().getItem(rs.getInt("sn"));
/* 168 */         if (cItem == null) {
           continue;
         }
/* 171 */         Item item = toItem(cItem, rs.getInt("uniqueid"), rs.getString("from"));
/* 172 */         gifts.add(new Pair(item, rs.getString("message")));
/* 173 */         this.uniqueids.add(Integer.valueOf(item.getUniqueId()));
/* 174 */         List<Integer> packages = CashItemFactory.getInstance().getPackageItems(cItem.getItemId());
/* 175 */         if (packages != null && packages.size() > 0) {
/* 176 */           for (Iterator<Integer> iterator = packages.iterator(); iterator.hasNext(); ) { int packageItem = ((Integer)iterator.next()).intValue();
/* 177 */             CashItemInfo pack = CashItemFactory.getInstance().getSimpleItem(packageItem);
/* 178 */             if (pack != null)
/* 179 */               addToInventory(toItem(pack, rs.getString("from")));  }

           continue;
         }
/* 183 */         addToInventory(item);
       }


/* 187 */       rs.close();
/* 188 */       ps.close();
/* 189 */       ps = con.prepareStatement("DELETE FROM `gifts` WHERE `recipient` = ?");
/* 190 */       ps.setInt(1, this.characterId);
/* 191 */       ps.executeUpdate();
/* 192 */       ps.close();
/* 193 */       save(con);
/* 194 */     } catch (SQLException sqle) {
/* 195 */       sqle.printStackTrace();
     }
/* 197 */     return gifts;
   }

   public boolean canSendNote(int uniqueid) {
/* 201 */     return this.uniqueids.contains(Integer.valueOf(uniqueid));
   }

   public void sendedNote(int uniqueid) {
/* 205 */     for (int i = 0; i < this.uniqueids.size(); i++) {
/* 206 */       if (((Integer)this.uniqueids.get(i)).intValue() == uniqueid) {
/* 207 */         this.uniqueids.remove(i);
       }
     }
   }

   public void save(DruidPooledConnection con) throws SQLException {
/* 213 */     List<Pair<Item, MapleInventoryType>> itemsWithType = new ArrayList<>();

/* 215 */     for (Item item : this.inventory) {
/* 216 */       itemsWithType.add(new Pair(item, ItemConstants.getInventoryType(item.getItemId())));
     }

/* 219 */     this.factory.saveItems(con, itemsWithType, this.accountId);
   }
 }


/* Location:              C:\PGX\Server.jar!\server\cashshop\CashShop.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */